<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>加载百度地图2</title>
    <link rel="stylesheet" href="../v6.4.3/css/ol.css">
  </head>
  <body>
    <div id="map" style="height: 500px;"></div>

    <label>Shape type &nbsp;</label>
    <select name="" id="type">
      <option value="">Select</option>
      <option value="Point">Point</option>
      <option value="LineString">LineString</option>
      <option value="Polygon">Polygon</option>
      <option value="Circle">Circle</option>
      <option value="Square">Square</option>
      <option value="Box">Box</option>
      <option value="None">None</option>
    </select>

    <script src="../v6.4.3/build/ol.js"></script>
    <script>
      // DOM对象变量
      let typeSelect = document.getElementById('type')
      
      //画图的Layer
      let vectorSource = new ol.source.Vector()
      let vectorLayer = new ol.layer.Vector({
        source: vectorSource
      })

      let draw
      function addInteraction() {
        let type = typeSelect.value;
        if (type && type !== 'None') {
          let geometryFunction;
          switch (type) {
            case 'Square':
              type = 'Circle';
              geometryFunction = ol.interaction.Draw.createRegularPolygon(4);
              break;
            case 'Box':
              type = 'Circle';
              geometryFunction = ol.interaction.Draw.createBox();
              break;
          }
          //初始化绘图控件
          draw = new ol.interaction.Draw({
            source: vectorSource,
            type: type,
            geometryFunction: geometryFunction
          })
          map.addInteraction(draw)
        }
      }

      // 事件绑定
      typeSelect.addEventListener('change', () => {
        map.removeInteraction(draw)
        addInteraction()
      })

      //自定义分辨率和瓦片坐标系
      var resolutions = []
      var maxZoom = 18
      //计算百度使用的分辨率
      for(var i = 0; i <= maxZoom; i++) {
        resolutions[i] = Math.pow(2, maxZoom - i)
      }
      var tilegrid = new ol.tilegrid.TileGrid({
        origin: [0, 0],
        resolutions: resolutions
      })

      //百度地图图层
      var baiduMapLayer = new ol.layer.Tile({
        source: new ol.source.XYZ({
          tilePixedRatio: 2,
          tileGrid: tilegrid,
          tileUrlFunction: function(tileCoord) {
            let z = tileCoord[0]
            let x = tileCoord[1]
            let y = -tileCoord[2] - 1
            if(x < 0) {
              x = 'M' + (-x)
            }
            if(y < 0) {
              y = 'M' + (-y)
            }
            return 'http://online3.map.bdimg.com/onlinelabel/?qt=tile&x=' + x + '&y=' + y + '&z=' + z + '&styles=pl&udt=20160321&scaler=1&p=0'
          }
        })
      })
      console.log(baiduMapLayer.getSource().getTileGrid())
      let map = new ol.Map({
        target: 'map',
        layers: [
          baiduMapLayer, vectorLayer
        ],
        view: new ol.View({
          center: ol.proj.fromLonLat([113.26, 23.01]),
          zoom: 14
        })
      })
    </script>
  </body>
</html>